package com.lmxdawn.api.dnf.controller;

import cn.hutool.core.date.DateUtil;
import com.lmxdawn.api.common.res.BaseResponse;
import com.lmxdawn.api.common.session.SessionContextHolder;
import com.lmxdawn.api.common.util.ResultVOUtils;
import com.lmxdawn.api.dnf.dto.CommonReq;
import com.lmxdawn.api.dnf.entity.WithdrawAccount;
import com.lmxdawn.api.dnf.service.IWithdrawAccountService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

@RestController
@RequestMapping("/web/dnf/withdraw-account")
public class WithdrawAccountController {

    @Resource
    private IWithdrawAccountService withdrawAccountService;

    @PostMapping("/save")
    public BaseResponse save(@RequestBody CommonReq<WithdrawAccount> account) {
        try {
            WithdrawAccount content = account.getContent();
            String userId = SessionContextHolder.get().getId();
            content.setUserId(userId);
            content.setCreateBy(userId);
            content.setCreateTime(DateUtil.date());
            return ResultVOUtils.success(withdrawAccountService.save(content));
        } catch (Exception e) {
            return ResultVOUtils.error(500, "创建失败，账户已存在");
        }
    }

    @GetMapping("/list")
    public BaseResponse list() {
        String userId = SessionContextHolder.get().getId();
        return ResultVOUtils.success(withdrawAccountService.lambdaQuery()
                .eq(WithdrawAccount::getUserId, userId)
                .list());
    }

    @PostMapping("/delete/{id}")
    public BaseResponse delete(@PathVariable Long id) {
        return ResultVOUtils.success(withdrawAccountService.removeById(id));
    }
    
    @PostMapping("/detail/{id}")
    public BaseResponse detail(@PathVariable Long id) {
        return ResultVOUtils.success(withdrawAccountService.getById(id));
    }
}